<?php
function betin( $p, $money, $rate, $sub_rates, $u2, $u3, $u4, $u5, $stime, $cdisk )
{
    global $curruser;
    global $lotto_id;
    global $rate0_8num;
    global $conf;
    static $lotto6h = NULL;
    if ( !isset( $lotto6h ) )
    {
        $lotto6h = new lotto6h();
    }
    $disk = strpos( " abcd", $cdisk );
    if ( $disk == 0 )
    {
        return false;
    }
    $prot_start = $conf->get( "protect_start" );
    $protmod = $u5['protmod'];
    if ( 1 < $rate )
    {
        $fields = array();
        $fields['user_id'] = $curruser['id'];
        $fields['lotto_id'] = $lotto_id;
        $fields['createtime'] = $stime;
        $fields['bettype'] = $p[0];
        $fields['numbers'] = "'".$p[1]."'";
        $fields['amount'] = $money;
        $fields['rate'] = $rate;
        $fields['payoff'] = $money * ( $rate - 1 );
        $fields['disk'] = $disk;
        $fields['line_nums'] = "'".$p[1]."'";
        $fields['unitmoney'] = $money;
        $a1 = GetBack( $curruser['id'], $fields['bettype'], $disk, 1 );
        $a2 = GetBack( $u2['id'], $fields['bettype'], $disk, 2 );
        $a3 = GetBack( $u3['id'], $fields['bettype'], $disk, 3 );
        $a4 = GetBack( $u4['id'], $fields['bettype'], $disk, 4 );
        $a5 = GetBack( $u5['id'], $fields['bettype'], $disk, 5 );
        $fields['back1'] = $a1;
        $fields['back2'] = $a2;
        $fields['back3'] = $a3;
        $fields['back4'] = $a4;
        $fields['back5'] = $a5;
        $a6 = GetBack( $u5['supe_account'], $fields['bettype'], $disk, 255 );
        $fields['back6'] = $a6;
        $fields['acl2'] = $u2['id'];
        $fields['acl3'] = $u3['id'];
        $fields['acl4'] = $u4['id'];
        $fields['acl5'] = $u5['id'];
        $fields['mpercent'] = $u2['mpercent'];
        $fields['percent2'] = $u2['percent0'];
        $fields['percent3'] = min( $u3['percent1'], $u4['mpercent'] - $fields['mpercent'] - $fields['percent2'] );
        $fields['percent4'] = min( $u4['percent1'], $u5['mpercent'] - $fields['mpercent'] - $fields['percent2'] - $fields['percent3'] );
        $fields['upercent'] = 100 - $fields['mpercent'] - $fields['percent2'] - $fields['percent3'] - $fields['percent4'];
        if ( 0 <= $fields['percent3'] && 0 <= $fields['percent4'] && 0 <= $fields['upercent'] )
        {
            $fields['r2'] = $money * $fields['percent2'] / 100;
            $fields['r3'] = $money * $fields['percent3'] / 100;
            $fields['r4'] = $money * $fields['percent4'] / 100;
            $fields['r6'] = $money * $fields['upercent'] / 100;
            if ( intval( $conf->get( "reat0_open" ) ) == 1 && $u5['reat0_open'] == 1 )
            {
                if ( $fields['bettype'] == 0 || $fields['bettype'] == 24 )
                {
                    $fields['rate1'] = $curruser['rate0'];
                    $fields['rate2'] = $u2['rate0'];
                    $fields['rate3'] = $u3['rate0'];
                    $fields['rate4'] = $u4['rate0'];
                }
                else if ( in_array( $fields['bettype'], array( 1, 2, 3, 20, 28, 30, 47 ) ) )
                {
                    if ( !isset( $rate0_8num ) )
                    {
                        $rate0_8num = 0;
                    }
                    $fields['rate1'] = $curruser['rate0'] * $rate0_8num;
                    $fields['rate2'] = $u2['rate0'] * $rate0_8num;
                    $fields['rate3'] = $u3['rate0'] * $rate0_8num;
                    $fields['rate4'] = $u4['rate0'] * $rate0_8num;
                }
            }
            $fields['rate5'] = $sub_rates;
            $fields['ip'] = "'".getIP( )."'";
            $newid = db_insert( "bet", $fields );
            if ( !$newid )
            {
                return false;
            }
			//扣除额度
		    getdb('update k_user set balance=balance-'.$money.' where user_id='.$curruser['id']);
			$rs=getdb("select user_id from k_user_bet where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
			if(!$rs->eof)
				getdb("update k_user_bet set amount_6h=amount_6h+".$money." where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
			else
				getdb("insert into k_user_bet set amount_6h=amount_6h+".$money." ,user_id='".$curruser['id']."',lotto_id='$lotto_id'");
			check_autorate( $lotto_id, $p[0], $p[1], $money * ( $fields['upercent'] / 100 ) );
            $lotto6h->getBet_6h( $newid );
        }
        else
        {
            err( "占成参数设置错误，注单无法存档！" );
        }
    }
    else
    {
        return false;
    }
    return true;
}

function betin_2t2b( $p, $money, $rate, $order, $u2, $u3, $u4, $u5, $stime, $cdisk )
{
    global $curruser;
    global $lotto_id;
    global $conf;
    static $lotto6h = NULL;
    if ( !isset( $lotto6h ) )
    {
        $lotto6h = new lotto6h();
    }
    $disk = strpos( " abcd", $cdisk );
    if ( $disk == 0 )
    {
        return false;
    }
    $prot_start = $conf->get( "protect_start" );
    $protmod = $u5['protmod'];
    if ( 1 < $rate )
    {
        $fields = array();
        $fields['user_id'] = $curruser['id'];
        $fields['lotto_id'] = $lotto_id;
        $fields['createtime'] = $stime;
        $fields['bettype'] = $p[0];
        $fields['numbers'] = "'".$order." ".$p[1]."'";
        $fields['amount'] = $money;
        $fields['rate'] = $rate;
        $fields['payoff'] = $money * ( $rate - 1 );
        $fields['disk'] = $disk;
        $fields['line_nums'] = "'".$order." ".$p[1]."'";
        $fields['unitmoney'] = $money;
        $a1 = GetBack( $curruser['id'], $fields['bettype'], $disk, 1 );
        $a2 = GetBack( $u2['id'], $fields['bettype'], $disk, 2 );
        $a3 = GetBack( $u3['id'], $fields['bettype'], $disk, 3 );
        $a4 = GetBack( $u4['id'], $fields['bettype'], $disk, 4 );
        $a5 = GetBack( $u5['id'], $fields['bettype'], $disk, 5 );
        $fields['back1'] = $a1;
        $fields['back2'] = $a2;
        $fields['back3'] = $a3;
        $fields['back4'] = $a4;
        $fields['back5'] = $a5;
        $a6 = GetBack( $u5['supe_account'], $fields['bettype'], $disk, 255 );
        $fields['back6'] = $a6;
        $fields['acl2'] = $u2['id'];
        $fields['acl3'] = $u3['id'];
        $fields['acl4'] = $u4['id'];
        $fields['acl5'] = $u5['id'];
        $fields['mpercent'] = $u2['mpercent'];
        $fields['percent2'] = $u2['percent0'];
        $fields['percent3'] = min( $u3['percent1'], $u4['mpercent'] - $fields['mpercent'] - $fields['percent2'] );
        $fields['percent4'] = min( $u4['percent1'], $u5['mpercent'] - $fields['mpercent'] - $fields['percent2'] - $fields['percent3'] );
        $fields['upercent'] = 100 - $fields['mpercent'] - $fields['percent2'] - $fields['percent3'] - $fields['percent4'];
        if ( $fields['percent3'] < 0 || $fields['percent4'] < 0 || $fields['upercent'] < 0 )
        {
            err( "占成参数设置错误，注单无法存档！" );
            return false;
        }
        $fields['r2'] = $money * $fields['percent2'] / 100;
        $fields['r3'] = $money * $fields['percent3'] / 100;
        $fields['r4'] = $money * $fields['percent4'] / 100;
        $fields['r6'] = $money * $fields['upercent'] / 100;
        $fields['ip'] = "'".getIP( )."'";
        $newid = db_insert( "bet", $fields );
        if ( !$newid )
        {
            return false;
        }
		$rs=getdb("select user_id from k_user_bet where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
		if(!$rs->eof)
			getdb("update k_user_bet set amount_6h=amount_6h+".$money." where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
		else
			getdb("insert into k_user_bet set amount_6h=amount_6h+".$money." ,user_id='".$curruser['id']."',lotto_id='$lotto_id'");

	    check_autorate( $lotto_id, $p[0], "{$order} {$p['1']}", $money * ( $fields['upercent'] / 100 ) );
        $lotto6h->getBet_6h( $newid );
    }
    else
    {
        err( "赔率设置错误，注单无法存档！" );
        return false;
    }
    return true;
}

include_once( "class_6h/class.lotto6h.php" );
?>